import { ZYSDK } from "../../../ZYSDK";
import AbstractRuntimeView from "../../common/abstract/mvc/AbstractRuntimeView";
import { soundMgr } from "../../common/def/sound";
import Event from "../../common/manager/EventClick";
import { ZYEventMgr } from "../../common/manager/YLEventMgr";
import gameModel from "../../common/model/gameModel";
import LayaUtils from "../../common/util/LayaUtils";
import MyTools from "../../common/util/MyTools";
import { ADMgr } from "../../common/wxComponent/ADManager";

/**
 * 
 * @author: xj.huang
 * @date: 2021/08/02 09:38:31
 */
export default class GameStartView extends AbstractRuntimeView<null, null> {
    /**场景加载界面 */
    private loadBg: Laya.Image;
    /**更多游戏 */
    private MoreGame: Laya.Image;
    /**开始游戏 */
    private StartGame: Laya.Image;
    private logoSkin: Laya.Image;
    private TwoSIde: Laya.Image;
    private viewType = "GameStartView";
    constructor() {
        super()
    }
    onOnce(param) {
        this.logoSkin.skin = ZYSDK.config.homeLogoUrl;
        this.gameConfig.box_conf.game.currIndex = 0;
        soundMgr.isMuteMusic = gameModel.userData.hasMusic;
        soundMgr.isMuteSound = gameModel.userData.hasSound;
        // 
        this.onAddEvent();
        this.addAppIcon();
        this.onShowViode();
        this.onResetIcon()
      
    }
    onEnable(){
        if(this.param?.type=="endView")this.onGameReadyEvent()
    }
    /**添加事件 */
    onAddEvent() {
        this.onSetActive(false)
        // this.MoreGame.visible = Laya.Browser.onWeiXin;
        if (!this.gameConfig.box_conf.game.order[0]) this.MoreGame.visible = false;
        ZYEventMgr.on(GameEvent.GAME2SDK_READY,this,this.onGameReadyEvent)
        Event.onClick(this.MoreGame, this, this.onMoreGame);
        Event.onClick(this.StartGame, this, this.onStartGame);
    }
    onResetIcon() {
        if (ZYSDK.config.startGameIcon != "" && ZYSDK.config.startGameIcon) {
            this.StartGame.skin = ZYSDK.config.startGameIcon;
        }
        if (ZYSDK.config.moreGameIcon != "" && ZYSDK.config.moreGameIcon) {
            this.MoreGame.skin = ZYSDK.config.moreGameIcon;
        }
    }
    onSetActive(v) {
        this.StartGame.visible = v;
        if (Laya.Browser.onWeiXin || !this.gameConfig.box_conf.game.order[0]) {
            this.MoreGame.visible = v
        }else{ 
            this.MoreGame.visible =false
        }
        // if(ZYSDK.config.isVerify) this.MoreGame.visible =true
    }
    /**监听游戏开始事件--Game端 */
    onGameReadyEvent() {
        this.loadBg.visible = false;
        this.onSetActive(true)
    }
    onShowViode() {
        Laya.timer.once(2000, this, () => {
            ADMgr.wxVideoAd.showVoide(1)
        })
    }
    /**点击更多界面按钮 */
    onMoreGame() {
        this.playClickSound()
        this.asyncExportOpen(this.gameConfig.box_conf.game, this.viewType)
    }
    /**点击开始游戏按钮 */
    private async onStartGame() {
        this.playClickSound()
        await ADMgr.wxVideoAd.showVoide(2)
        await this.UIMgrAsyncOpen(ViewRes.VIEW_GAME);
        this.onClose()
    }
    /**显示左右banner */
    addAppIcon() {
        let random = Math.random() * 100;
        this.gameConfig.box_conf.home.is_banner
        if (random > this.gameConfig.box_conf.home.is_box) {
            this.TwoSIde.visible = false;
        } else {
            this.TwoSIde.visible = true;
            this.initTwoSide();
            Laya.timer.loop(20000, this, this.initTwoSide);
        }
    }

    /**显示 */
    private initTwoSide() {
        let ran = MyTools.getRandomNumInt(0, this.gameConfig.game_box.length);
        for (let i = 0; i < this.TwoSIde.numChildren; i++) {
            let TowChilds = this.TwoSIde.getChildAt(i) as Laya.Image;
            (TowChilds.getChildByName("game_tex") as Laya.Image).skin = this.gameConfig.game_box[(ran + i) % (this.gameConfig.game_box.length)].icon_url;
            TowChilds.name = this.gameConfig.game_box[(ran + i) % (this.gameConfig.game_box.length)].id + "";
            TowChilds.on(Laya.Event.CLICK, this, this.clickImage, [TowChilds]);
        }
    }
    /**增加跳转点击事件 */
    public clickImage(item) {
        let data = null;
        for (let j = 0; j < this.gameConfig.game_box.length; j++) {
            if (item.name == this.gameConfig.game_box[j].id) {
                data = this.gameConfig.game_box[j];
                break;
            }
        }
    }
    onTimePlayAni() {
        Laya.timer.loop(2000, this, () => {
            let temp = MyTools.getRandomNumInt(0, 5);
            LayaUtils.aniShake(this.TwoSIde.getChildAt(temp), -15, 40, 3)
        })
    }

}